﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common.Library;

namespace VipSys
{
    public partial class OrgnizationMag : System.Web.UI.Page
    {
        private List<Orgnization> m_lstOrgnization;
        protected void Page_Load(object sender, EventArgs e)
        {
            SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            if (sysAccount == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('登录超时，请重新登录!')</script>");
                return;
            }
            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "BindGrid":
                        BindGrid();
                        return;
                    case "GetParentOrg":
                        GetParentOrg();
                        return;
                    case "BindRoleType":
                        BindRoleType();
                        return;
                    case "Add":
                        Add();
                        return;
                    case "Edit":
                        Edit();
                        return;
                    case "Del":
                        Del();
                        return;
                    default:
                        return;
                }
            }
        }

        private void Add()
        {
            string jsons = "";
            Orgnization orgnization = new Orgnization();
            var OrgName = Request["OrgName"];
            var ParentID = Request["ParentID"];
            var Describe = Request["Describe"];
            var RoleType = Request["RoleType"];
            orgnization.OrgName = OrgName;
            orgnization.ParentID = ParentID;
            orgnization.Describe = Describe;
            orgnization.RoleType = RoleType;
            short shRet = OrgnizationAccess.Instance.Add(orgnization);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":true,msg:\"新增失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"新增成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
 
        private void Edit()
        {
            string jsons = "";
            Orgnization orgnization = new Orgnization();
            var OrgName = Request["OrgName"];
            var ParentID = Request["ParentID"];
            var Describe = Request["Describe"];
            var RoleType = Request["RoleType"];
            var ID = Request["ID"];
            orgnization.ID = Int32.Parse(ID);
            orgnization.OrgName = OrgName;
            orgnization.ParentID = ParentID;
            orgnization.Describe = Describe;
            orgnization.RoleType = RoleType;
            short shRet = OrgnizationAccess.Instance.Edit(orgnization);
            if (shRet != SystemData.ReturnValue.OK)
            {
                jsons = "{\"success\":true,msg:\"编辑失败\"}";
            }
            else
            {
                jsons = "{\"success\":true,msg:\"编辑成功\"}";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void Del()
        {
            string jsons = "";
            Orgnization orgnization = new Orgnization();
            var deletesplit = Request["deletesplit"];
            foreach (var ID in deletesplit.Split(','))
            {
                orgnization.ID = int.Parse(ID);
                short shRet = OrgnizationAccess.Instance.Del(orgnization);
                if (shRet != SystemData.ReturnValue.OK)
                {
                    jsons = "{\"success\":true,msg:\"删除失败\"}";
                    break;
                }
            }
            if (string.IsNullOrEmpty(jsons))
                jsons = "{\"success\":true,msg:\"删除成功\"}";
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void BindGrid()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            Orgnization query = new Orgnization();
            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string MenuTitle = Request.Form["MenuTitle"];
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = OrgnizationAccess.Instance.GetOrgnizationPageList(query, ref this.m_lstOrgnization);

                foreach (Orgnization item in this.m_lstOrgnization)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("OrgName", item.OrgName);
                    json.AddItem("ParentID", item.ParentID);
                    if (!string.IsNullOrEmpty(item.ParentID))
                    {
                        Orgnization result = new Orgnization();
                        result.ID = int.Parse(item.ParentID);
                        OrgnizationAccess.Instance.GetOrgnization(result, ref result);
                        item.ParentName = result.OrgName;
                    }
                    json.AddItem("ParentName", item.ParentName);
                    json.AddItem("Describe", item.Describe);
                    json.AddItem("RoleType", item.RoleType);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = OrgnizationAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        private void GetParentOrg()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            Orgnization query = new Orgnization();

            query.Start = Request.Form["start"];
            query.Limit = Request.Form["limit"];
            string OrgName = Request.Form["OrgName"];
            string RoleType = Request.Form["RoleType"];
            query.OrgName = OrgName;
            query.RoleType = RoleType;
            if (query.Start != null && query.Limit != null)
            {
                json.success = true;
                short shRet = OrgnizationAccess.Instance.GetOrgnizationPageList(query, ref this.m_lstOrgnization);

                foreach (Orgnization item in this.m_lstOrgnization)
                {
                    json.AddItem("ID", item.ID.ToString());
                    json.AddItem("OrgName", item.OrgName);
                    json.AddItem("RoleType", item.RoleType);
                    json.ItemOk();
                }
                int totalCount = 0;
                shRet = OrgnizationAccess.Instance.GetTotalCount(query, ref totalCount);
                json.totlalCount = totalCount;
                if (json.totlalCount > 0)
                {
                    jsons = json.ToString();
                }
                else
                {
                    jsons = @"[]";
                }
            }
            else
            {
                jsons = @"{success:false}";
            }

            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
        public void BindRoleType()
        {
            JSONHelper json = new JSONHelper();
            json.success = true;
            string jsons = "";
            json.success = true;
            List<SysCombo> lstRoleType = new List<SysCombo>();
            ComboAccess.Instance.GetRoleTypes(ref lstRoleType);
            foreach (var row in lstRoleType)
            {
                json.AddItem("value", row.Value);
                json.AddItem("name", row.Name);
                json.ItemOk();
            }
            json.totlalCount = lstRoleType.Count();
            if (json.totlalCount > 0)
            {
                jsons = json.ToString();
            }
            else
            {
                jsons = @"[]";
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }

    }
}